<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AngularJS&middot;Hello AngularJS</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
    <style>
        .demo{
            margin-bottom:10px;
            border-bottom:1px solid #1b926c;
        }
    </style>
</head>
<body>

 <div class="container">
     <div class="row">
         <nav class="navbar navbar-default navbar-static-top">
             <div class="container-fluid">
                 <div class="navbar-header">
                     <a class="navbar-brand" href="../index.html">首页</a>
                 </div>
                 <ul class="nav navbar-nav">
                    <li class="active"><a href="javascript:void(0)">Link</a></li>
                    <li><a href="javascript:void(0)">Link2</a></li>
                 </ul>
             </div>
         </nav>
     </div>
     <div class="row demo" ng-app="MyAPP">
         <h3>Angular Service&Controller&Directive三大指令整合示例</h3>
         <div ng-controller="GalleryController">
             <button ng-click="getMovies()">显示数据</button>
             <p ng-if="counter>0">
                 图片总数为：{{counter}}
             </p>
             <ul>
                 <li ng-repeat="m in movies">
                     <p><img src="{{m.thumb}}" alt=""></p>
                     <p>{{m.desc}}</p>
                 </li>
             </ul>
         </div>
     </div>
 </div>
 </div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script>
    var app=angular.module("MyAPP",[]);
    app.service("movieservice",function($http){
       this.fetch=function(){
           return $http.get("data/photos.json");
       }
    });

    app.controller("GalleryController",function($scope,movieservice){
       $scope.movies=[];
       $scope.getMovies=function(){
           movieservice.fetch().then(function(resp){
               $scope.movies=resp.data.movies;
               console.log($scope.movies);
           },function(err){
               console.log("请求错误");
           });
       };
        $scope.$watch("movies.length",function(value){
            console.log(value);
            $scope.counter=value;
        });
    });

    //TOOD 尝试着使用指令显示图片总数，不知道为什么，指令中老是提示undefined
//    app.directive('counter', function(photos) {
//        return {
//            link: function($scope, $element, attrs) {
//
//                $scope.$watch("movies.length",function(value){
//                    $element.text("total photos:"+value);
//                });
//            }
//        };
//    });

</script>
